package top.littlejiang.onlinexam.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamExam;
import top.littlejiang.onlinexam.entity.OlexamExamPaper;
import top.littlejiang.onlinexam.entity.OlexamQuestions;
import top.littlejiang.onlinexam.service.OlexamExamPaperService;
import top.littlejiang.onlinexam.vo.PaperQueryVO;

import java.io.Serializable;
import java.util.List;

/**
 * @author chen
 * @Description
 * @since 2021/3/20 16:53
 */
@Controller
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    OlexamExamPaperService paperService;

    /**
     * 试卷列表 -》教师
     * @param paperQuery
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    @RequiresPermissions("olexam:exampaper:list")
    public Result getList(@RequestBody PaperQueryVO paperQuery){
        if (StringUtils.isBlank(paperQuery.getFkCourseName()) && StringUtils.isBlank(paperQuery.getFkExamName())){
            return Result.fail("请选择课程或考试！");
        }
        List<OlexamExamPaper> list = paperService.getList(paperQuery);
        return Result.ok(list);
    }

    /**
     * 禁用试卷
     * @param ids
     * @return
     */
    @Log("禁用试卷")
    @PostMapping("/del")
    @ResponseBody
    @RequiresPermissions("olexam:exampaper:list")
    public Result delete(@RequestBody List<Serializable> ids){
        paperService.logicDeleteById(ids);
        return Result.ok();
    }

    /**
     * 新增或修改
     * @param examPaper
     * @return
     */
    @Log("新增/更新试卷")
    @PostMapping("/insertorupdate")
    @ResponseBody
    public Result insertOrUpdate(@RequestBody OlexamExamPaper examPaper){
        paperService.insertOrUpdate(examPaper);
        return Result.ok();
    }

    /**
     * 根据uuid 获取试卷 -》学生考试用
     * @param exam
     * @return
     */
    @PostMapping("/getbyuuid")
    @ResponseBody
    public Result getByUuid(@RequestBody OlexamExam exam){
        List<OlexamQuestions> questionsList = paperService.getByUuid(exam);
        JSONObject rst = new JSONObject();
        rst.put("questionsList", questionsList);
        rst.put("sysTime", System.currentTimeMillis());
        return Result.ok(rst);
    }

}
